//
//  CYUserShare.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/28.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYUserShare.h"

@interface CYUser()

@end

@implementation CYUser
static CYUser *_user;
static dispatch_once_t onceToken;
+ (instancetype)shareUser{
    if (!_user) {
        dispatch_once(&onceToken, ^{
            _user = [[CYUser alloc] init];
        });
    }
    return _user;
}

+ (void)initializeUsernameAndToken{
    CYUser *user = [CYUser shareUser];
    NSString *username = [CYCache readDataWithKey:@"username"];
    NSString *token = [CYCache readDataWithKey:@"token"];
    NSString * coin = [CYCache readDataWithKey:@"coin"];
    NSString *account = [CYCache readDataWithKey:@"account"];
    NSString *bankApiImgUrl = [CYCache readDataWithKey:@"bankApiImgUrl"];
    NSString *bankName = [CYCache readDataWithKey:@"bankName"];
    NSString *keTi = [CYCache readDataWithKey:@"keTi"];
    
    user.username = username;
    user.token = token;
    user.account = account;
    user.bankName = bankName;
    user.bankApiImgUrl = bankApiImgUrl;
    user.coin = [coin floatValue];
    user.keTi = [keTi floatValue];
    
}

- (void)setUsername:(NSString *)username{
    if (_username != username) {
        _username = username;
        if (!username)
            [CYCache deleteDataWithKey:@"username"];
        else
            [CYCache saveData:username forKey:@"username" synchronize:YES];
    }
}

- (void)setToken:(NSString *)token{
    if (_token != token) {
        _token = token;
        if (!token)
            [CYCache deleteDataWithKey:@"token"];
        else
            [CYCache saveData:token forKey:@"token" synchronize:YES];
    }
}

- (void)setAccount:(NSString *)account{
    if (_account != account) {
        _account = account;
        [CYCache saveData:_account forKey:@"account" synchronize:YES];
    }
}

- (void)setBankApiImgUrl:(NSString *)bankApiImgUrl{
    if (_bankApiImgUrl != bankApiImgUrl) {
        _bankApiImgUrl = bankApiImgUrl;
        [CYCache saveData:bankApiImgUrl forKey:@"bankApiImgUrl" synchronize:YES];
    }
}

- (void)setBankName:(NSString *)bankName{
    if (_bankName != bankName) {
        _bankName = bankName;
        [CYCache saveData:bankName forKey:@"bankName" synchronize:YES];
    }
}

- (void)setKeTi:(float)keTi{
    if (_keTi != keTi) {
        _keTi = keTi;
        [CYCache saveData:@(keTi) forKey:@"keTi" synchronize:YES];
    }
}

- (void)setCoin:(float)coin{
    if (_coin != coin) {
        _coin = coin;
        [CYCache saveData:@(coin) forKey:@"coin" synchronize:YES];
    }
}
@end
